#include "NinePatch.h"

#include "PLATFORM.h"
#ifdef _ANDROID_PLATFORM__
#include "SDL_rect.h"
#else
#include <SDL2/SDL_rect.h>
#endif

#include "_Image.h"

NinePatch::NinePatch(Image image) : image(image) {
  SDL_Surface* imageSurface =
      _Image::getInstance().getImageSurface(image.getId());

  //分区线
  int topX = -1;
  int topWidth = -1;
  int leftY = -1;
  int leftHeight = -1;

  //内容区域线
  int bottomX = -1;
  int bottomWidth = -1;
  int rightY = -1;
  int rightHeight = -1;

  int imageWidth = imageSurface->w;
  int imageHeight = imageSurface->h;

  //是否为8位色（Uint8）
  if (imageSurface->format->BitsPerPixel == 8) {
    Uint8* pixels = (Uint8*)(imageSurface->pixels);  //当前像素数组
    //横线
    for (unsigned i = 0; i < imageWidth; i++) {
      //顶部横线
      //如果此像素非0
      if (pixels[i]) {
        //如果是第一次
        if (topX == -1) {
          topX = i;  //记录顶部横线的左端横坐标
        } else if (topX != -1) {
          //如果不是第一次
          topWidth =
              i - topX +
              1;  //记录顶部横线的宽度（多次更新，最终达到真正宽度，感觉有点笨）
        }
      }

      //底部横线
      //如果此像素非0
      if (pixels[(imageHeight - 1) * imageWidth + i]) {
        //如果是第一次
        if (bottomX == -1) {
          bottomX = i;  //记录底部横线的左端横坐标
        } else if (bottomX != -1) {
          //如果不是第一次
          bottomWidth =
              i - bottomX +
              1;  //记录底部横线的宽度（多次更新，最终达到真正宽度，感觉有点笨）
        }
      }
    }

    //竖线
    for (unsigned i = 0; i <= (imageHeight - 1) * imageWidth; i += imageWidth) {
      //左边竖线
      //如果此像素非0
      if (pixels[i]) {
        //如果是第一次
        if (leftY == -1) {
          leftY = i / imageWidth;  //记录左边竖线的上端纵坐标
        } else if (leftY != -1) {
          //如果不是第一次
          leftHeight = i / imageWidth - leftY + 1;
        }
      }

      //右边竖线
      //如果此像素非0
      if (pixels[imageWidth - 1 + i]) {
        //如果是第一次
        if (rightY == -1) {
          rightY = i / imageWidth;  //记录右边竖线的上端纵坐标
        } else if (rightY != -1) {
          //如果不是第一次
          rightHeight = i / imageWidth - rightY + 1;
        }
      }
    }
  }

  //是否为16位色（Uint16）
  if (imageSurface->format->BitsPerPixel == 16) {
    Uint16* pixels = (Uint16*)(imageSurface->pixels);  //当前像素数组
    //横线
    for (unsigned i = 0; i < imageWidth; i++) {
      //顶部横线
      //如果此像素非0
      if (pixels[i]) {
        //如果是第一次
        if (topX == -1) {
          topX = i;  //记录顶部横线的左端横坐标
        } else if (topX != -1) {
          //如果不是第一次
          topWidth =
              i - topX +
              1;  //记录顶部横线的宽度（多次更新，最终达到真正宽度，感觉有点笨）
        }
      }

      //底部横线
      //如果此像素非0
      if (pixels[(imageHeight - 1) * imageWidth + i]) {
        //如果是第一次
        if (bottomX == -1) {
          bottomX = i;  //记录底部横线的左端横坐标
        } else if (bottomX != -1) {
          //如果不是第一次
          bottomWidth =
              i - bottomX +
              1;  //记录底部横线的宽度（多次更新，最终达到真正宽度，感觉有点笨）
        }
      }
    }

    //竖线
    for (unsigned i = 0; i <= (imageHeight - 1) * imageWidth; i += imageWidth) {
      //左边竖线
      //如果此像素非0
      if (pixels[i]) {
        //如果是第一次
        if (leftY == -1) {
          leftY = i / imageWidth;  //记录左边竖线的上端纵坐标
        } else if (leftY != -1) {
          //如果不是第一次
          leftHeight = i / imageWidth - leftY + 1;
        }
      }

      //右边竖线
      //如果此像素非0
      if (pixels[imageWidth - 1 + i]) {
        //如果是第一次
        if (rightY == -1) {
          rightY = i / imageWidth;  //记录右边竖线的上端纵坐标
        } else if (rightY != -1) {
          //如果不是第一次
          rightHeight = i / imageWidth - rightY + 1;
        }
      }
    }
  }
  //是否为32位色（Uint32）
  if (imageSurface->format->BitsPerPixel == 32) {
    Uint32* pixels = (Uint32*)(imageSurface->pixels);  //当前像素数组
    //横线
    for (unsigned i = 0; i < imageWidth; i++) {
      //顶部横线
      //如果此像素非0
      if (pixels[i]) {
        //如果是第一次
        if (topX == -1) {
          topX = i;  //记录顶部横线的左端横坐标
        } else if (topX != -1) {
          //如果不是第一次
          topWidth =
              i - topX +
              1;  //记录顶部横线的宽度（多次更新，最终达到真正宽度，感觉有点笨）
        }
      }

      //底部横线
      //如果此像素非0
      if (pixels[(imageHeight - 1) * imageWidth + i]) {
        //如果是第一次
        if (bottomX == -1) {
          bottomX = i;  //记录底部横线的左端横坐标
        } else if (bottomX != -1) {
          //如果不是第一次
          bottomWidth =
              i - bottomX +
              1;  //记录底部横线的宽度（多次更新，最终达到真正宽度，感觉有点笨）
        }
      }
    }

    //竖线
    for (unsigned i = 0; i <= (imageHeight - 1) * imageWidth; i += imageWidth) {
      //左边竖线
      //如果此像素非0
      if (pixels[i]) {
        //如果是第一次
        if (leftY == -1) {
          leftY = i / imageWidth;  //记录左边竖线的上端纵坐标
        } else if (leftY != -1) {
          //如果不是第一次
          leftHeight = i / imageWidth - leftY + 1;
        }
      }

      //右边竖线
      //如果此像素非0
      if (pixels[imageWidth - 1 + i]) {
        //如果是第一次
        if (rightY == -1) {
          rightY = i / imageWidth;  //记录右边竖线的上端纵坐标
        } else if (rightY != -1) {
          //如果不是第一次
          rightHeight = i / imageWidth - rightY + 1;
        }
      }
    }
  }

  //处理区域
  // left_top
  left_top.x = 1;
  left_top.y = 1;
  left_top.width = topX - 1;
  left_top.height = leftY - 1;

  // top
  top.x = topX;
  top.y = 1;
  top.width = topWidth;
  top.height = left_top.height;

  // right_top
  right_top.x = topX + topWidth;
  right_top.y = 1;
  right_top.width = imageWidth - 1 - right_top.x;
  right_top.height = left_top.height;

  // left
  left.x = 1;
  left.y = leftY;
  left.width = left_top.width;
  left.height = leftHeight;

  // center
  center.x = topX;
  center.y = leftY;
  center.width = topWidth;
  center.height = leftHeight;

  // right
  right.x = right_top.x;
  right.y = leftY;
  right.width = right_top.width;
  right.height = leftHeight;

  // left_bottom
  left_bottom.x = 1;
  left_bottom.y = leftY + leftHeight;
  left_bottom.width = left.width;
  left_bottom.height = imageHeight - 1 - left_bottom.y;

  // bottom
  bottom.x = topX;
  bottom.y = left_bottom.y;
  bottom.width = topWidth;
  bottom.height = left_bottom.height;

  // right_bottom
  right_bottom.x = right.x;
  right_bottom.y = bottom.y;
  right_bottom.width = right.width;
  right_bottom.height = bottom.height;

  //内容区域
  contentWidth = bottomWidth;
  contentHeight = rightHeight;
  contentToTop = rightY - 1;
  contentToLeft = bottomX - 1;
  contentToBottom = imageHeight - 1 - (rightY + rightHeight);
  contentToRight = imageWidth - 1 - (bottomX + bottomWidth);
}

//不考虑内容区域
//在x,y绘制宽width,高height的9patch图
void NinePatch::draw(int x, int y, int width, int height) {
  image.draw(left_top, x, y, left_top.width, left_top.height);
  image.draw(top, x + left_top.width, y,
             width - left_top.width - right_top.width, top.height);
  image.draw(right_top, x + width - right_top.width, y, right_top.width,
             right_top.height);
  image.draw(left, x, y + left_top.height, left.width,
             height - left_top.height - left_bottom.height);
  image.draw(center, x + left_top.width, y + left_top.height,
             width - left_top.width - right_top.width,
             height - left_top.height - left_bottom.height);
  image.draw(right, x + width - right_top.width, y + left_top.height,
             right.width, height - left_top.height - left_bottom.height);
  image.draw(left_bottom, x, y + height - left_bottom.height, left_bottom.width,
             left_bottom.height);
  image.draw(bottom, x + left_bottom.width, y + height - left_bottom.height,
             width - left_top.width - right_top.width, left_bottom.height);
  image.draw(right_bottom, x + width - right_top.width,
             y + height - left_bottom.height, right_bottom.width,
             right_bottom.height);
}

void NinePatch::drawArea(Rect area, int x, int y, int width, int height) {
  image.drawArea(area, left_top, Rect{x, y, left_top.width, left_top.height});
  image.drawArea(area, top,
                 Rect{x + left_top.width, y,
                      width - left_top.width - right_top.width, top.height});
  image.drawArea(
      area, right_top,
      Rect{x + width - right_top.width, y, right_top.width, right_top.height});
  image.drawArea(area, left,
                 Rect{x, y + left_top.height, left.width,
                      height - left_top.height - left_bottom.height});
  image.drawArea(area, center,
                 Rect{x + left_top.width, y + left_top.height,
                      width - left_top.width - right_top.width,
                      height - left_top.height - left_bottom.height});
  image.drawArea(
      area, right,
      Rect{x + width - right_top.width, y + left_top.height, right.width,
           height - left_top.height - left_bottom.height});
  image.drawArea(area, left_bottom,
                 Rect{x, y + height - left_bottom.height, left_bottom.width,
                      left_bottom.height});
  image.drawArea(
      area, bottom,
      Rect{x + left_bottom.width, y + height - left_bottom.height,
           width - left_top.width - right_top.width, left_bottom.height});
  image.drawArea(
      area, right_bottom,
      Rect{x + width - right_top.width, y + height - left_bottom.height,
           right_bottom.width, right_bottom.height});
}

void NinePatch::drawContent(int x, int y, int contentWidth, int contentHeight) {
  draw(x, y, contentWidth + contentToLeft + contentToRight,
       contentHeight + contentToTop + contentToBottom);
}

void NinePatch::drawContentArea(Rect area, int x, int y, int contentWidth,
                                int contentHeight) {
  drawArea(area, x, y, contentWidth + contentToLeft + contentToRight,
           contentHeight + contentToTop + contentToBottom);
}
